﻿$(document).ready(function () {
    $core.recaptcha.load();
    $('#submit-button').click(function () {
        var contactForm = $('form.contact-form');
        var firstNameField = $('#first-name-field');
        var lastNameField = $('#last-name-field');
        var emailField = $('#email-address-field');
        var phoneField = $('#phone-number-field');
        var serviceField = $('#services-field');
        var challengeField = $('#recaptcha_challenge_field');
        var responseField = $('#recaptcha_response_field');
        responseField.addClass('required');

        var isValid = $core.validate(contactForm);
        if (!isValid) {
            $('#app-msg')
                .removeClass('ui-state-highlight')
                .addClass('ui-state-error')
                .text('Please correct the above fields');
            return;
        }

        var model = {
            Recaptcha: {
                ChallengeField: challengeField.val(),
                ResponseField: responseField.val()
            },
            FirstName: firstNameField.val(),
            LastName: lastNameField.val(),
            EmailAddress: emailField.val(),
            PhoneNumber: phoneField.val(),
            ServiceCode: serviceField.val()
        }

        $core.blockPartial(contactForm, 'Saving...');

        $core.post({
            url: $core.makeRelative('home/CollectContactInfo'),
            data: model,
            success: function (response) {
                if (response.Success) {
                    $('#app-msg')
                        .removeClass('ui-state-error')
                        .addClass('ui-state-highlight')
                        .text(response.Message);
                    $core.recaptcha.destroy();
                    $core.unblock(contactForm);
                    var table = $('form.contact-form table');
                    var thankyou = $('<div/>');
                    thankyou
                        .css('height', table.height())
                        .css('width', table.width())
                        .addClass('thank-you');
                    table.replaceWith(thankyou);
                }
                else {
                    $('#app-msg')
                        .removeClass('ui-state-highlight')
                        .addClass('ui-state-error')
                        .text(response.Message);
                    if (response.StackTrace != null) {
                        alert(response.StackTrace);
                    }
                    $core.recaptcha.reload();
                    $core.unblock(contactForm);
                }
            },
            failure: function (error) {
                $('#app-msg')
                    .removeClass('ui-state-highlight')
                    .addClass('ui-state-error')
                    .text('Could not save your information. Try again.');
                $core.recaptcha.reload();
                $core.unblock(contactForm);
            }
        });
    });
});